//
//  OSSManager.m
//  JXNetwork
//
//  Created by hgs on 2019/10/11.
//  Copyright © 2019 hgs All rights reserved.
//

#import "OSSManager.h"
#import "OSSWrapper.h"
#import "JXHttpRequest.h"
#import "JXRequestManager+JX.h"
#import <AliyunOSSiOS/AliyunOSSiOS.h>

@interface  OSSManager ()
@property (nonatomic, strong) JXHttpRequest *bbsNet;
@end

@implementation OSSManager

+ (instancetype)sharedManager {
    static OSSManager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[OSSManager alloc] init];
        _manager.bbsNet = [[JXHttpRequest alloc] init];
        _manager.uploadChance = 30;
        _manager.updateFiles = [NSMutableArray new];
        _manager.downFiles = [NSMutableArray new];
    });
    
    return _manager;
}

+ (NSString *)todayTime
{
    NSDate *date = [NSDate date]; // 获得时间对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd"];
    NSString *strDay = [dateFormatter stringFromDate:date];
    return strDay;
}

/**
 * today
 * uploadCount
 */
+ (NSDictionary *)currentUploadConfig {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentUploadConfig"]) {
        NSDictionary *currentUploadConfig = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentUploadConfig"];
        if ([currentUploadConfig objectForKey:@"today"]) {
            if ([[self todayTime] isEqualToString:[currentUploadConfig objectForKey:@"today"]]) {
                return currentUploadConfig;
            }
        }
    }
    return nil;
}

+ (void)autoSaveUploadCount:(int)uploadCount{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];
    [dic setObject:[NSNumber numberWithInt:uploadCount] forKey:@"uploadCount"];
    [dic setObject:[self todayTime] forKey:@"today"];
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"currentUploadConfig"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 * today
 * publishCount
 */
+ (NSDictionary *)currentPublishConfig {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentPublishConfig"]) {
        NSDictionary *currentUploadConfig = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentPublishConfig"];
        if ([currentUploadConfig objectForKey:@"today"]) {
            if ([[self todayTime] isEqualToString:[currentUploadConfig objectForKey:@"today"]]) {
                return currentUploadConfig;
            }
        }
    }
    return nil;
}
+ (void)autoSavePublishCount:(int)publishCount {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];
    [dic setObject:[NSNumber numberWithInt:publishCount] forKey:@"publishCount"];
    [dic setObject:[self todayTime] forKey:@"today"];
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"currentPublishConfig"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)getOSSServer:(void (^)(BOOL isSuccess))callBack {
    if (!self.imageClient &&
        JXRequestConfig.sharedManager.uid) {
        NSDictionary *params = @{@"app":@"nearby",
                                 @"uid":JXRequestConfig.sharedManager.uid};
        NSDictionary *param = @{@"class":@"bottle",
                                @"method":@"STS",
                                @"msgid":@(150),
                                @"params":params};
        weak_block_self;
        [self.bbsNet netBody:param result:^(BOOL isSuccess, id object) {
            if (isSuccess) {
                NSDictionary *result = [object objectForKey:@"result"];
                weakSelf.bucketName = [result objectForKey:@"BUCKET_NAME"];
                weakSelf.bucketNameVoice = [result objectForKey:@"BUCKET_NAME_VOICE"];
                weakSelf.UrlVoicePrefix = [result objectForKey:@"URL_VOICE_PREFIX"];
                NSString *OSS_ENDPOINT = [result objectForKey:@"OSS_ENDPOINT"];
                NSString *OSS_STS_URL = [result objectForKey:@"OSS_STS_URL"];
                weakSelf.uploadChance = [[result objectForKey:@"uploadChance"] intValue];
//                weakSelf.bucketName = @"nearby-photo";
//                OSS_ENDPOINT = @"oss-cn-shenzhen.aliyuncs.com";
//                OSS_STS_URL = @"http://sts-nearby.gtxxz.com:7110";
                // initialize credential provider,which auto fetch and update sts info from sts url.
                OSSAuthCredentialProvider *credentialProvider = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:OSS_STS_URL];
                
                // set config for oss client networking
                OSSClientConfiguration *cfg = [[OSSClientConfiguration alloc] init];
                
                weakSelf.imageClient = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT credentialProvider:credentialProvider clientConfiguration:cfg];
                if (callBack) {
                    callBack(isSuccess);
                }
            } else if (callBack) {
                callBack(NO);
            }
        }];
    } else if(JXRequestConfig.sharedManager.uid){
        if (callBack) {
            callBack(YES);
        }
    } else {
        if (callBack) {
            callBack(NO);
        }
    }
}

/**
 * today
 * downCount
 */
+ (NSDictionary *)currentDownConfig {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentDownConfig"]) {
        NSDictionary *currentDownConfig = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentDownConfig"];
        if ([currentDownConfig objectForKey:@"today"]) {
            if ([[self todayTime] isEqualToString:[currentDownConfig objectForKey:@"today"]]) {
                return currentDownConfig;
            }
        }
    }
    return nil;
}
+ (void)autoSaveDownCount:(int)downCount {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:3];
    [dic setObject:[NSNumber numberWithInt:downCount] forKey:@"downCount"];
    [dic setObject:[self todayTime] forKey:@"today"];
    [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"currentDownConfig"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
